package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tangkun.environment.R;
import com.tangkun.environment.activity.LocationDemo;
import com.tangkun.environment.activity.TiJiaoXunChaShuJuActivity;
import com.tangkun.environment.activity.WuRanYuanActivity;
import com.tangkun.environment.activity.YiChangShuJuTiJaoActivity;
import com.tangkun.environment.entity.gongzuo.GongZuoBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: GongZuoGridViewAdapter
 * @Description: 工作GridView adapter
 * @date 2016-08-20 上午11:57:36
 */
public class GongZuoGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<GongZuoBean> listData = new ArrayList<GongZuoBean>();

    /**
     * 企业ID
     */
    private String enterpriseid = "";

    public String getEnterpriseid() {
        return enterpriseid;
    }

    public void setEnterpriseid(String enterpriseid) {
        this.enterpriseid = enterpriseid;
    }

    public GongZuoGridViewAdapter(Context context, List<GongZuoBean> listData) {
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int i) {
        return listData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_gongzuo_gridview, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (listData != null) {
            //0:全部污染源  1：附近污染源  2：提交数据  3：审核  4：举报异常  5：统计  6：更多
            switch (listData.get(position).tag) {
                case 0://全部污染源
                    holder.tvGongzuoGridview.setText("全部污染源");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_quanbuwuranyuan);
                    break;
                case 1://附近污染源
                    holder.tvGongzuoGridview.setText("附近污染源");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_fujinwuranyuan);
                    break;
                case 2://提交数据
                    holder.tvGongzuoGridview.setText("提交数据");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_tijiaoshuju);
                    break;
                case 3://审核
                    holder.tvGongzuoGridview.setText("审核");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_shenhe);
                    break;
                case 4://举报异常
                    holder.tvGongzuoGridview.setText("举报异常");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_jubaoyichang);
                    break;
                case 5://统计
                    holder.tvGongzuoGridview.setText("统计");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_tongji);
                    break;
                case 6://更多
                    holder.tvGongzuoGridview.setText("更多功能");
                    holder.ivGongzuoGridview.setImageResource(R.mipmap.ic_gengduogongneng);
                    break;
            }

            //跳转相应页面
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //0:全部污染源  1：附近污染源  2：提交数据  3：审核  4：举报异常  5：统计  6：更多
                    Intent intent;
                    switch (listData.get(position).tag) {
                        case 0://全部污染源
                            intent = new Intent(context, WuRanYuanActivity.class);
                            context.startActivity(intent);
                            break;
                        case 1://附近污染源
                            intent = new Intent(context, LocationDemo.class);
                            context.startActivity(intent);
                            break;
                        case 2://提交数据
                            intent = new Intent(context, TiJiaoXunChaShuJuActivity.class);
                            intent.putExtra("id", getEnterpriseid());
                            context.startActivity(intent);
                            break;
                        case 3://审核
                            intent = new Intent(context, WuRanYuanActivity.class);
                            intent.putExtra("owner", "1");
                            context.startActivity(intent);
                            break;
                        case 4://举报异常
                            intent = new Intent(context, YiChangShuJuTiJaoActivity.class);
                            context.startActivity(intent);
                            break;
                        case 5://统计
                            intent = new Intent(context, YiChangShuJuTiJaoActivity.class);
                            context.startActivity(intent);
                            break;
                        case 6://更多
                            showToast("即将开放");
                            break;
                    }
                }
            });
        }
        return convertView;

    }

    private Toast mToast;

    /**
     * showToast
     *
     * @param msg
     */
    private void showToast(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 0);//居中显示
        } else {
            mToast.setText(msg);
            mToast.setGravity(Gravity.CENTER, 0, 0);//居中显示
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    static class ViewHolder {
        @BindView(R.id.iv_gongzuo_gridview)
        ImageView ivGongzuoGridview;
        @BindView(R.id.tv_gongzuo_gridview)
        TextView tvGongzuoGridview;
        @BindView(R.id.ll_gongzuo_gridview)
        LinearLayout llGongzuoGridview;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
